﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Q.Schedulers
{
    public class WaitWorkItem : IExecutableWorkItem
    {
        private WaitCallback _callback;
        private object _state;

        public WaitWorkItem(string id, WaitCallback callback, object state)
            : this(id, DateTime.MinValue, callback, state, 1)
        { }

        public WaitWorkItem(string id, DateTime time,
            WaitCallback callback, object state, int repeat)
        {
            _callback = callback;
            _state = state;

            Identifier = id;
            ExecutionTime = time;
            RepeatCount = repeat;
        }

        #region IExecutableWorkItem 成员

        public DateTime ExecutionTime { get; set; }

        public string Identifier { get; set; }

        public bool IsCancellationRequested { get; set; }

        public int RepeatCount { get; set; }

        public void Execute()
        {
            if (_callback != null)
            {
                _callback(_state);

                _callback = null;
                _state = null;
            }
        }

        #endregion
    }
}
